package com.junwell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.Result;
import com.junwell.entity.Car;
import com.junwell.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;


    @Autowired
    private JedisPool jedisPool;



    @GetMapping("findPage")
    public IPage<Car> findPage(PageDto pageDto){
        IPage<Car> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("car_number"  , pageDto.getQueryString());
        page  = carService.page(page, queryWrapper);
        return page;
    }

    @PostMapping("add")
    public Result add(@RequestBody Car car){
        boolean add = carService.add(car);
        if (add){
            return Result.succ("成功");
        }else {
            return Result.fail("添加失败");
        }
    }



    @GetMapping("{id}")
    public Result viewOne(@PathVariable("id")Integer id){
        Car car = carService.viewOne(id);
        if (car !=null){
            return Result.succ(200,"成功",car);
        }else {
            return Result.fail("失败");
        }
    }


    @PostMapping("update")
    public Result update(@RequestBody Car car){
        boolean update = carService.update(car);
        if (update){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id ){
        boolean delete = carService.delete(id);
        if (delete){
            return Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }

    @GetMapping("drivers")
    public Result ways(){
        Jedis jedis = jedisPool.getResource();
        List<GeoCoordinate> list = jedis.geopos("station", "李载", "张三", "李四", "王五", "赵六", "赵虎", "张龙");
        jedis.close();
        return Result.succ(list);
    }

    /*public Result drivers(){

    }
*/
}
